package com.kzz.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowInsets;
import android.view.WindowInsetsController;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.DialogFragment;

import com.kzz.dialoglibraries.DialogBottomSetDateInterface;
import com.kzz.dialoglibraries.DialogPopuSetDateInterface;
import com.kzz.dialoglibraries.TimeInterface;
import com.kzz.dialoglibraries.dialog.DialogCreate;
import com.kzz.dialoglibraries.dialog.DialogFragmentBottom;
import com.kzz.dialoglibraries.dialog.DialogFragmentRight;
import com.kzz.dialoglibraries.popupWindow.PopupWindowBase;

import java.text.MessageFormat;


public class MainActivity extends AppCompatActivity {

    private Button btDialog;
    private Button btPopupWindow;
    private Button btDialogBottom,bt_dialog_right;
    private Activity mActivity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        hideNavigationBar();
        this.mActivity = this;
        btDialog = findViewById(R.id.bt_dialog);
        btPopupWindow = findViewById(R.id.bt_popup_window);
        btDialogBottom = findViewById(R.id.bt_dialog_bottom);
        bt_dialog_right = findViewById(R.id.bt_dialog_right);
        btDialog.setOnClickListener(view -> showNoNetworkDialog());
        btPopupWindow.setOnClickListener(view -> showPopupWindow());
        btDialogBottom.setOnClickListener(view -> showDialogBottom());
        bt_dialog_right.setOnClickListener(v -> showDialogRight());
    }

    /**
     * 提交没有网络的时候弹窗
     */
    private void showNoNetworkDialog() {
//        DialogCreate.Builder builder = new DialogCreate.Builder(mActivity);
//        mDialogCreate = builder
//        DialogCreate dd=    new DialogCreate.Builder(mActivity)
           new DialogCreate.Builder(mActivity)
                .setAddViewId(R.layout.dialog_contact_phone)
                   .setIsCloseDialogOutSide(false)
//                   .setBackgroundRes(R.color.theme)
//                   .setIsTransparent(true)
//                .setStyle(R.style.AlertDialogFullscreen)//activity全屏的时候要设置这个style
                   .isNavigationBarHide(true)
                   .isStatusBarHide(true)
                .setDialogSetDateInterface((inflaterView, dialogCreate) -> {
                    TextView tvMsg = inflaterView.findViewById(R.id.tv_dialog_msg);
                    TextView tvCancel = inflaterView.findViewById(R.id.tv_cancel);
                    TextView tvConfirm = inflaterView.findViewById(R.id.tv_confirm);
                    tvMsg.setText("当前无网络，将自动保存！\n请待有网络后提交。");
                    tvConfirm.setOnClickListener(v -> {
                        dialogCreate.dismiss();
                        Toast.makeText(mActivity, "确认", Toast.LENGTH_SHORT).show();
                    });
                    tvCancel.setOnClickListener(v -> {
                        dialogCreate.dismiss();
                        Toast.makeText(mActivity, "取消", Toast.LENGTH_SHORT).show();
                    });
                })
                .setMaxTime(15)
                .setTimeInterface(new TimeInterface() {
                    @Override
                    public void onTime(View inflaterView, int time) {
                        TextView tvConfirm = inflaterView.findViewById(R.id.tv_confirm);
                        tvConfirm.setText(MessageFormat.format("确认（{0}s)", time));
                    }

                    @Override
                    public void onEnd(Dialog dialog) {

                    }
                })
                .build().myShow();
//                .build();
//        dd.myShow();
    }

    private void showDialogBottom() {
//        DialogFragmentBottom.Builder builder = new DialogFragmentBottom.Builder();
//        mDialogBottom = builder
        new DialogFragmentBottom.Builder()
                .setAddViewId(R.layout.dialog_contact_phone)
                .isNavigationBarHide(true)
//                .isStatusBarHide(true)
//                .setTransparency(150)//默认：全屏dialog，如果设置这个透明度值后，上面的标题栏则不会被dialog挡住。
                .setDialogSetDateInterface(new DialogBottomSetDateInterface() {
                    @Override
                    public void setDate(View inflaterView, DialogFragment dialog) {
                        TextView tvMsg = inflaterView.findViewById(R.id.tv_dialog_msg);
                        TextView tvCancel = inflaterView.findViewById(R.id.tv_cancel);
                        TextView tvConfirm = inflaterView.findViewById(R.id.tv_confirm);
                        tvMsg.setText("当前无网络，将自动保存！\n请待有网络后提交。");
                        tvConfirm.setOnClickListener(v -> {
                            startActivity(new Intent(mActivity, TestActivity.class));
                            Toast.makeText(mActivity, "确认", Toast.LENGTH_SHORT).show();
                        });
                        tvCancel.setOnClickListener(v -> {
                            dialog.dismiss();
                            Toast.makeText(mActivity, "取消", Toast.LENGTH_SHORT).show();
                        });
                    }
                })
                .build().showSingle(this, getSupportFragmentManager(), "MainActivity");
//                .build().showNoDouble( getSupportFragmentManager(), "MainActivity");
//        mDialogBottom.showSingle(this,getSupportFragmentManager(), "MainActivity");
    }


    private void showDialogRight() {
//        DialogFragmentBottom.Builder builder = new DialogFragmentBottom.Builder();
//        mDialogBottom = builder
        new DialogFragmentRight.Builder()
                .setAddViewId(R.layout.dialog_right)
                .isNavigationBarHide(true)
//                .setTransparency(150)//默认：全屏dialog，如果设置这个透明度值后，上面的标题栏则不会被dialog挡住。
                .setDialogSetDateInterface(new DialogBottomSetDateInterface() {
                    @Override
                    public void setDate(View inflaterView, DialogFragment dialog) {
                        TextView tvMsg = inflaterView.findViewById(R.id.tv_dialog_msg);
                        TextView tvCancel = inflaterView.findViewById(R.id.tv_cancel);
                        TextView tvConfirm = inflaterView.findViewById(R.id.tv_confirm);
                        tvMsg.setText("当前无网络，将自动保存！\n请待有网络后提交。");
                        tvConfirm.setOnClickListener(v -> {
                            startActivity(new Intent(mActivity, TestActivity.class));
                            Toast.makeText(mActivity, "确认", Toast.LENGTH_SHORT).show();
                        });
                        tvCancel.setOnClickListener(v -> {
                            dialog.dismiss();
                            Toast.makeText(mActivity, "取消", Toast.LENGTH_SHORT).show();
                        });
                    }
                })
                .build().showNoDouble(getSupportFragmentManager(), "MainActivity");
//                .build().showNoDouble( getSupportFragmentManager(), "MainActivity");
//        mDialogBottom.showSingle(this,getSupportFragmentManager(), "MainActivity");
    }

    /**
     * 显示弹出窗口
     */
    private void showPopupWindow() {
        PopupWindowBase popupWindowBase = new PopupWindowBase(mActivity, btPopupWindow, R.layout.pop_seletct);
        DialogPopuSetDateInterface dialogSetDateInterface = new DialogPopuSetDateInterface() {
            @Override
            public void setDate(View inflaterView, PopupWindow popupWindow) {
                TextView tv001 = inflaterView.findViewById(R.id.tv_001);
                TextView tv002 = inflaterView.findViewById(R.id.tv_002);
                TextView tv003 = inflaterView.findViewById(R.id.tv_003);
                tv001.setOnClickListener(view -> Toast.makeText(mActivity, "tv001", Toast.LENGTH_SHORT).show());
                tv002.setOnClickListener(view -> Toast.makeText(mActivity, "tv002", Toast.LENGTH_SHORT).show());
                tv003.setOnClickListener(view -> Toast.makeText(mActivity, "tv003", Toast.LENGTH_SHORT).show());
            }
        };
//        popupWindowBase.setBackgroundResource(R.color.blace_00000000);
        popupWindowBase.setShowingInterface(isShowing -> {
            if (isShowing) {
                Toast.makeText(mActivity, "显示PopupWindow", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(mActivity, "关闭PopupWindow", Toast.LENGTH_SHORT).show();
            }
        });
        popupWindowBase.setCallback(dialogSetDateInterface);
        popupWindowBase.create();
    }

    /**
     * 获得屏幕宽度
     *
     * @param context context
     * @return 屏幕宽度
     */
    private int getScreenWidth(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        if (wm != null)
            wm.getDefaultDisplay().getMetrics(outMetrics);
        return outMetrics.widthPixels;
    }

    /**
     * 隐藏导航栏、状态栏
     */
    private void hideNavigationBar() {
//        val decorView = window.decorView
//        val uiOptions = (View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
//                or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY or View.SYSTEM_UI_FLAG_FULLSCREEN)
//        decorView.systemUiVisibility = uiOptions

        View decorView = getWindow().getDecorView();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            getWindow().setDecorFitsSystemWindows(false);
            WindowInsetsController controller = decorView.getWindowInsetsController();
            if (controller != null) {
                controller.hide(WindowInsets.Type.systemBars());
                controller.setSystemBarsBehavior(WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE);

            }
        } else {
            decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
            | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_FULLSCREEN);
        }
    }
}
